package org.dromara.basic.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.basic.domain.bo.PlaceBo;
import org.dromara.basic.domain.vo.PlaceVo;
import org.dromara.basic.domain.Place;
import org.dromara.basic.mapper.PlaceMapper;
import org.dromara.basic.service.IPlaceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 跑区设置Service业务层处理
 *
 * @author chl
 * @date 2024-08-30
 */
@RequiredArgsConstructor
@Service
public class PlaceServiceImpl implements IPlaceService {

    private final PlaceMapper baseMapper;

    /**
     * 查询跑区设置
     */
    @Override
    public PlaceVo queryById(Long placeId){
        return baseMapper.selectVoById(placeId);
    }

    /**
     * 查询跑区设置列表
     */
    @Override
    public TableDataInfo<PlaceVo> queryPageList(PlaceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Place> lqw = buildQueryWrapper(bo);
        Page<PlaceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询跑区设置列表
     */
    @Override
    public List<PlaceVo> queryList(PlaceBo bo) {
        LambdaQueryWrapper<Place> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Place> buildQueryWrapper(PlaceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Place> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlaceName()), Place::getPlaceName, bo.getPlaceName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceType()), Place::getPlaceType, bo.getPlaceType());
        lqw.eq(bo.getPlaceId()!=null, Place::getPlaceId, bo.getPlaceId());
        return lqw;
    }

    /**
     * 新增跑区设置
     */
    @Override
    public Boolean insertByBo(PlaceBo bo) {
        Place add = MapstructUtils.convert(bo, Place.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlaceId(add.getPlaceId());
        }
        return flag;
    }

    /**
     * 修改跑区设置
     */
    @Override
    public Boolean updateByBo(PlaceBo bo) {
        Place update = MapstructUtils.convert(bo, Place.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Place entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除跑区设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
